<h1 class="d-flex justify-content-between">
  <span class="text-nowrap me-2">
    <my-global-icon iconName="globe" aria-hidden="true"></my-global-icon>
    <span i18n>Runner jobs</span>
  </span>

  <a routerLink="/admin/system/runners/runners-list" class="peertube-button-link peertube-button-icon grey-button">
    <my-global-icon iconName="codesandbox" aria-hidden="true"></my-global-icon>
    <ng-container i18n>Remote runners</ng-container>
  </a>
</h1>

<p-table
  [value]="runnerJobs" [paginator]="totalRecords > 0" [totalRecords]="totalRecords" [rows]="rowsPerPage" [first]="pagination.start"
  [rowsPerPageOptions]="rowsPerPageOptions" [sortField]="sort.field" [sortOrder]="sort.order"
  [lazy]="true" (onLazyLoad)="loadLazy($event)" [lazyLoadOnInit]="false"
  [(selection)]="selectedRows" [showCurrentPageReport]="true" [currentPageReportTemplate]="getPaginationTemplate()"
  [expandedRowKeys]="expandedRows" dataKey="uuid"
>
  <ng-template pTemplate="header">
    <tr>
      <th scope="col" style="width: 40px">
        <p-tableHeaderCheckbox ariaLabel="Select all rows" i18n-ariaLabel></p-tableHeaderCheckbox>
      </th>
      <th scope="col" style="width: 40px;">
        <span i18n class="visually-hidden">More information</span>
      </th>
      <th scope="col" style="width: 120px;">
        <span i18n class="visually-hidden">Actions</span>
      </th>
      <th scope="col" i18n>UUID</th>
      <th scope="col" i18n>Type</th>
      <th scope="col" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="state">State <p-sortIcon field="state"></p-sortIcon></th>
      <th scope="col" style="width: 100px" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="priority">Priority <p-sortIcon field="priority"></p-sortIcon></th>
      <th scope="col" style="width: 100px" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="progress">Progress <p-sortIcon field="progress"></p-sortIcon></th>
      <th scope="col" i18n>Runner</th>
      <th scope="col" style="width: 200px;" i18n [ngbTooltip]="sortTooltip" container="body" pSortableColumn="createdAt">Created <p-sortIcon field="createdAt"></p-sortIcon></th>
    </tr>
  </ng-template>

  <ng-template pTemplate="caption">

    <div class="caption">
      <div class="left-buttons">
        <my-action-dropdown
          *ngIf="isInSelectionMode()" i18n-label label="Batch actions" theme="orange"
          [actions]="bulkActions" [entry]="selectedRows"
        >
        </my-action-dropdown>
      </div>

      <div class="ms-auto d-flex">
        <my-advanced-input-filter class="me-2" [filters]="inputFilters" (search)="onSearch($event)"></my-advanced-input-filter>

        <my-button i18n-label label="Refresh" icon="refresh" (click)="reloadData()"></my-button>
      </div>
    </div>
  </ng-template>

  <ng-template pTemplate="body" let-expanded="expanded" let-runnerJob>
    <tr [pSelectableRow]="runnerJob">
      <td class="checkbox-cell">
        <p-tableCheckbox [value]="runnerJob" ariaLabel="Select this row" i18n-ariaLabel></p-tableCheckbox>
      </td>

      <td class="expand-cell">
        <my-table-expander-icon [pRowToggler]="runnerJob" [expanded]="expanded"></my-table-expander-icon>
      </td>

      <td class="action-cell">
        <my-action-dropdown
          placement="bottom-right top-right left auto" container="body"
          i18n-label label="Actions" [actions]="actions" [entry]="runnerJob"
        ></my-action-dropdown>
      </td>

      <td>{{ runnerJob.uuid }}</td>
      <td>{{ runnerJob.type }}</td>
      <td>
        <span class="pt-badge" [ngClass]="getStateBadgeColor(runnerJob)">{{ runnerJob.state.label }}</span>
      </td>
      <td>{{ runnerJob.priority }}</td>

      <td>
        <ng-container *ngIf="runnerJob.progress">{{ runnerJob.progress }}%</ng-container>
      </td>

      <td>{{ runnerJob.runner?.name }}</td>
      <td>{{ runnerJob.createdAt }}</td>
    </tr>
  </ng-template>

  <ng-template pTemplate="rowexpansion" let-runnerJob>
    <tr>
      <td myAutoColspan>
        <div class="mt-2 fs-7 font-monospace">
          Parent job: {{ runnerJob.parent?.uuid || '-' }} <br />
          Processed on {{ (runnerJob.startedAt || '-') }} <br />
          Finished on {{ (runnerJob.finishedAt || '-') }} <br />
        </div>

        <div class="mt-2">
          <strong i18n>Payload:</strong>
          <pre>{{ runnerJob.payload }}</pre>
        </div>

        <div class="mt-2">
          <strong i18n>Private payload:</strong>
          <pre>{{ runnerJob.privatePayload }}</pre>
        </div>

        <pre *ngIf="runnerJob.error" class="text-danger" >{{ runnerJob.error }}</pre>
      </td>
    </tr>
  </ng-template>

  <ng-template pTemplate="emptymessage">
    <tr>
      <td myAutoColspan>
        <div class="no-results">
          <ng-container i18n>No runner jobs found.</ng-container>
        </div>
      </td>
    </tr>
  </ng-template>
</p-table>
